package com.xinongbest.service.impl;

import com.xinongbest.dao.UserDao;
import com.xinongbest.dao.impl.UserDaoImpl;
import com.xinongbest.entity.User;
import com.xinongbest.factory.BeanFactory;
import com.xinongbest.proxy.DynamicProxy;
import com.xinongbest.service.UserService;
import com.xinongbest.utils.TransactionManager;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    /** 传统方式使用new关键字，关联Dao接口对象 */
    // public UserDao userDao = new UserDaoImpl();
    /** 从对象容器中获取对象 */
    // public UserDao userDao = (UserDao) BeanFactory.getInstance().getBean("userDaoImpl");
    /**
     * 使用动态代理，抽取事务控制逻辑
     */
    // public UserDao userDao = (UserDao) DynamicProxy.getInstance().getJdkProxyObject(BeanFactory.getInstance().getBean("userDaoImpl"));

    private DynamicProxy dynamicProxy = (DynamicProxy) BeanFactory.getInstance().getBean("dynamicProxy");
    /**
     * 使用容器
     */
    public UserDao userDao = (UserDao) dynamicProxy.getJdkProxyObject(BeanFactory.getInstance().getBean("userDaoImpl"));


    @Override
    public List<User> getAllUsers() throws SQLException {
        return userDao.selectAllUser();
    }

    @Override
    public Integer updateUserById(User user) throws SQLException {
        return userDao.updateUserById(user);
    }

    @Override
    public Integer updateUserByIdByDynamicTrans(User user) throws SQLException {
        return userDao.updateUserByIdByDynamicTrans(user);
    }

    @Override
    public Integer updateUserByIdByDynamicTransNew(User user) throws SQLException {
        return userDao.updateUserByIdByDynamicTransNew(user);
    }
}
